package org.example.controller;

import com.google.common.collect.ImmutableMap;
import org.example.dto.CategoryDto;
import org.example.dto.SubCategoryDto;
import org.example.entity.SysCategory;
import org.example.entity.SysSubCategory;
import org.example.model.CategoryModel;
import org.example.model.DeptModel;
import org.example.model.PageBean;
import org.example.service.SysCategoryService;
import org.example.service.SysDeptService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/category")
public class SysCategoryController {

    @Resource
    private SysCategoryService sysCategoryService;

    @GetMapping("/show/{page}")
    public PageBean<SysCategory> showAll(@PathVariable("page") Integer page) {
        System.out.println("page________>>"+page);
        return sysCategoryService.getAll(page);
    }
    @PostMapping("/sub/x/")
    public PageBean<SysSubCategory> showAllById(@RequestBody SubCategoryDto subCategoryDto) {
//@RequestBody SubCategoryDto subCategoryDto

        System.out.println("controller-page---->"+subCategoryDto.getCategoryId()+subCategoryDto.getPage());
        return sysCategoryService.getAllById(subCategoryDto.getCategoryId(),subCategoryDto.getPage());
    }

    //获取编辑数据回显
    @GetMapping("/edit/{id}")
    public SysCategory getById(@PathVariable("id") Integer id) {

        System.out.println("edit---->id的值为："+id);
        return sysCategoryService.getById(id);
    }
    @PutMapping
    public Map<String, Object> editCategory(@RequestBody CategoryDto categoryDto){
        sysCategoryService.editCategory(categoryDto);
        return ImmutableMap.of("code", 1, "msg", "success"); }

    @DeleteMapping("/{id}")
    public Map<String, Object> deleteById(@PathVariable("id") Integer id){
        sysCategoryService.deleteById(id);
        return ImmutableMap.of("code", 1, "msg", "success"); }

    @PostMapping
    public Map<String, Object> addSubCategory(@RequestBody CategoryDto categoryDto){
        System.out.println("xxxxxxxxxx"+categoryDto);
        sysCategoryService.addCategory(categoryDto);
        return ImmutableMap.of("code", 1, "msg", "success"); }
}
